package com.github.tvbox.osc.bean;

import com.github.tvbox.osc.util.Utils;
import com.google.common.collect.Lists;
import com.google.gson.Gson;
import com.google.gson.annotations.SerializedName;
import com.thoughtworks.xstream.annotations.XStreamAsAttribute;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

public class Daily implements Serializable {

    @SerializedName(value = "temperature")
    private List<Temperature> temperatures;
    @SerializedName(value = "skycon")
    private List<Skycon> skycons;
    @SerializedName(value = "skycon_08h_20h")
    private List<Skycon> skycon0820;
    @SerializedName(value = "skycon_20h_32h")
    private List<Skycon> skycon2032;
    @SerializedName(value = "wind")
    private List<Wind> winds;

    public class Temperature implements Serializable {
        @XStreamAsAttribute
        private String date;
        @SerializedName(value = "max")
        private double max;
        @SerializedName(value = "min")
        private double min;
    }

    public class Skycon implements  Serializable{
        @XStreamAsAttribute
        private String date;
        @XStreamAsAttribute
        private String code;
        @XStreamAsAttribute
        private String desc;
    }

    public class Wind implements Serializable{
        @XStreamAsAttribute
        private String date;
        @SerializedName(value = "avg")
        private Map<String,Object> avg;
    }

    public List<Temperature> getTemperatures() {
        return temperatures;
    }

    public List<Skycon> getSkycons() {
        return skycons;
    }

    public List<Wind> getWinds() {
        return winds;
    }

    public List<Weather> get4DayWeather(){
        List<Weather> dats = Lists.newArrayList();
        if(temperatures!=null && temperatures.size()>0 && skycons!=null && skycons.size()>0){
            int num = 6;
            if(skycons.size()<=num){
                num = skycons.size();
            }
            for (int i = 1; i < num; i++) {
                Skycon skycon = skycons.get(i);
                String sDate = Utils.getSubstring(skycon.date,10);
                Weather weather = new Weather();
                for (Temperature temperature: temperatures) {
                    if(temperature.date.equals(skycon.date)){
                        weather.setMax(Utils.subZeroAndDot(String.valueOf(temperature.max)));
                        weather.setMin(Utils.subZeroAndDot(String.valueOf(temperature.min)));
                        break;
                    }
                }
                if(winds!=null&&winds.size()>0){
                    for (Wind wind:winds) {
                        if(wind.date.equals(skycon.date)){
                            weather.setDirection(Utils.subZeroAndDot(Utils.getMapValue(wind.avg,"direction","")));
                            weather.setDirectionDesc(Utils.getMapValue(wind.avg,"direction_desc",""));
                            weather.setSpeed(Utils.subZeroAndDot(Utils.getMapValue(wind.avg,"speed","")));
                            weather.setSpeedDesc(Utils.getMapValue(wind.avg,"speed_desc",""));
                            break;
                        }
                    }
                }
                weather.setDate(sDate);
                weather.setCode(skycon.code);
                weather.setDesc(skycon.desc);
                dats.add(weather);
            }

        }
        return dats;
    }
}
